package com.cf.studentsociety.web;

import com.cf.studentsociety.dao.MemberDao;
import com.cf.studentsociety.dao.StudentDao;
import com.cf.studentsociety.dao.impl.MemberDaoImpl;
import com.cf.studentsociety.dao.impl.StudentDaoImpl;
import com.cf.studentsociety.entity.Member;
import com.cf.studentsociety.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "student", urlPatterns = "/student/*")
public class StudentServlet extends RouteServlet {

    private StudentDao studentDao = new StudentDaoImpl();
    private MemberDao memberDao = new MemberDaoImpl();
    @Override
    protected Class getMyClass() {
        return this.getClass();
    }
    public String login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Student stu = null;
        Member member = null;
        try {
            stu = studentDao.login(account, password);

            if (stu != null) {
                member = memberDao.queryByNumber(stu.getStudentNumber());
                System.out.println(member);
                HttpSession session = req.getSession();
                session.setAttribute("student", stu.getStudentNumber());
                session.setAttribute("position", member );
                System.out.println(stu.getStudentNumber());
                res.sendRedirect("/studentSociety/society/mainIndex");
            } else {
                req.getRequestDispatcher("/index.jsp?message=account or password is wrong").forward(req, res);
            }
        }catch (SQLException throwables) {
            handleException(throwables,req,res);
        }
        return "direct";
    }


    public String register(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        // 打印所有请求参数，检查是否正确接收到
        System.out.println("Received studentNumber: " + req.getParameter("studentNumber"));
        System.out.println("Received studentName: " + req.getParameter("studentName"));
        System.out.println("Received gender: " + req.getParameter("gender"));
        System.out.println("Received birthday: " + req.getParameter("birthday"));
        System.out.println("Received password: " + req.getParameter("password"));

        // 获取前端传来的参数
        String studentNumber = req.getParameter("studentNumber");
        String studentName = req.getParameter("studentName");
        String gender = req.getParameter("gender");
        String birthdayStr = req.getParameter("birthday");  // birthday是字符串
        System.out.println(birthdayStr);
        String password = req.getParameter("password");
        String graduated = new String("1");

        // 将字符串日期转换为Date类型
        Date birthday = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  // 假设日期格式是yyyy-MM-dd
        try {
            birthday = sdf.parse(birthdayStr);
        } catch (ParseException e) {
            throw new RuntimeException("日期格式无效", e);  // 如果日期格式不对，抛出异常
        }

        try {
            studentDao.addStudent(studentNumber, studentName, gender, birthday, password, graduated);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


    public String queryStudent(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String name = req.getParameter("name");
        String number = req.getParameter("number");
        List<Student> students = null;
        try {
            students = studentDao.queryStudent(name, number);
            req.setAttribute("students", students);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("exception", throwables);
            return "error"; // 如果有异常，跳转到错误页面
        }

        // 确保字符编码正确设置
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        // 转发请求至目标页面
        req.getRequestDispatcher("/member/addPage").forward(req, res);
        return null; // 请求已转发，不需要再返回一个视图名
    }

//    public String logout (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            request.getSession().invalidate();
//            response.sendRedirect("/index.jsp");
//        return null;
//    }


}
